<?php
/**
 * Puts user input into system.
 *
 *
 **/

require_once 'thread.php';
require_once 'tpl.php';

if( !is_string($_REQUEST['type']) ) {
  // error BREAK
} 

// function variables are tempting
//   however, pose risk and should be comtemplated
//   after a full review of standards has been set
//   to insure no code injection
switch( $_REQUEST['type'] ) {
  case 'POST': 
          handlePost(); 
          break;
  case 'THREAD':
          handleThread();
          break;
  case 'FORUM': 
          handleFora(); 
          break;
  default:
}

// A temp fix. This should be a smart referal.
tplHead();
echo <<<EOE
<a href="spit.php" >Thank You.</a>
EOE;
tplFoot();


// //  abstraction is tempting, again we'll wait for optimization
function handlePost() {
  if ( isset($_REQUEST['action']['new']) || isset($_REQUEST['action']['edit']) ){
    if( $_POST['tnum'] > 0 ) {
      $tnum = (int)$_POST['tnum'];
      if( $_POST['pnum'] > 0 ) { 
        $pnum = (int)$_POST['pnum'];
      } else {
        $pnum = -1;
      }
    } else if ( $_POST['fnum'] > 0 ) {
      $tnum = (int)$_POST['fnum'];
      $pnum = 0;
    } else {
      // BREAKAGE
      die("ohz noes");
    }

    setPost( $tnum, $pnum, clean($_POST['title']), clean($_POST['datum']), $options );
  
  } elseif( isset($_REQUEST['action']['delete']) 
            && isset($_REQUEST['pnum'])
            && isset($_REQUEST['tnum']) ) {
    deletePost( (int)$_REQUEST['tnum'], (int)$_REQUEST['pnum'] );  
  }
}

function handleThread() {
  if( isset($_REQUEST['action']['delete']) 
            && isset($_REQUEST['tnum'])
            && isset($_REQUEST['fnum']) ) {
    deleteThread( (int)$_REQUEST['fnum'], (int)$_REQUEST['tnum'] );  
  }
}

function handleFora() {
  if( isset($_POST['title']) ) {
    $fnum = ($_POST['fnum'] > 0 ) ? (int)$_POST['fnum'] : -1;

    if( isset($_REQUEST['action']['new']) || isset($_REQUEST['action']['edit']) ) {
      setForum( -1, $fnum, clean($_POST['title']), clean($_POST['datum']) );
    } elseif (isset($_REQUEST['action']['delete'])) {
      deleteForum( -1, $fnum );
    } else {
      die("BREAKAGE");
    }
  }
}


?>

